﻿Chenxin.UI.StructComboList = Chenxin.extend(null, {
    tbxWidth: 45,
    inModelType: 'val',
    comboContainerId: {
        city: '#city',
        company: '#company',
        businessDistrict: '#businessdistrict',
        departs: '#departs',
        employee: '#employee'
    },
    /**
    *@description 城市选中之后的时间，默认传递选中的新值
    */
    cityAfterSelected: Chenxin.emptyFn,

    /**
    *@description 公司选中之后的时间，默认传递选中的新值
    */
    companyAfterSelected: Chenxin.emptyFn,
    /**
    *@description 商圈选中之后的时间，默认传递选中的新值
    */
    businessDistrictAfterSelected: Chenxin.emptyFn,
    /**
    *@description 门店选中之后的时间，默认传递选中的新值
    */
    departsAfterSelected: Chenxin.emptyFn,

    /**
    *@description 城市Item加载之后,默认传递Items数组
    */
    cityAfterLoadItem: Chenxin.emptyFn,

    /**
    *@description 公司Items加载之后,默认传递Items数组
    */
    companyAfterLoadItem: Chenxin.emptyFn,
    comboNameConfig: {},
    comboTxtModelNameConfig: {},
    comboConfig: {
        city: {},
        company: {},
        business: {},
        departs: {},
        emp: {}
    }, //配置项
    createComboList: function() {
        var _this = this;

        var config = {};
        Chenxin.apply(config, this.comboConfig.city);


        //城市
        Chenxin.applyIf(config, {
            param: { op: 'getcity' },
            inModelType: this.inModelType,
            containerId: this.comboContainerId.city,
            name: this.comboNameConfig.city,
            txtModelName: this.comboTxtModelNameConfig.city,
            tbxWidth: this.tbxWidth,
            afterLoadItems: function() {
                if (this.items.length == 1) {
                    this.addPleaseSelected = false;
                    this.initValue = this.items[0].val;
                    _this.company.param.fatherid = this.items[0].val;
                    _this.company.reload();
                } else {
                    this.addPleaseSelected = true;
                }

                if (_this.cityAfterLoadItem) {
                    _this.cityAfterLoadItem.call(_this, this.items);
                }
            },
            afterSetValue: function(val) {
                if (val == this.defVal) {
                    _this.company.clearItems();
                    _this.businessDistrict.clearItems();
                    _this.departs.clearItems();
                    _this.emp.clearItems();
                }
            },
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    _this.company.clearItems();
                } else {
                    _this.company.param.fatherid = this.getValue();
                    _this.company.reload();
                }
                _this.businessDistrict.clearItems();
                _this.departs.clearItems();
                _this.emp.clearItems();

                if (_this.cityAfterSelected) {
                    _this.cityAfterSelected.call(_this, this.getValue());
                }
            }
        });

        this.city = new Chenxin.UI.StructCombo(config);


        config = {};
        Chenxin.apply(config, this.comboConfig.company);
        Chenxin.applyIf(config, {
            autoLoad: false,
            param: { op: 'getcompany' },
            tbxWidth: this.tbxWidth,
            inModelType: this.inModelType,
            name: this.comboNameConfig.company,
            txtModelName: this.comboTxtModelNameConfig.company,
            containerId: this.comboContainerId.company,
            afterLoadItems: function() {
                if (this.items.length == 1) {
                    this.addPleaseSelected = false;
                    this.initValue = this.items[0].val;
                    _this.businessDistrict.param.fatherid = this.items[0].val;
                    _this.businessDistrict.reload();
                } else {
                    this.addPleaseSelected = true;
                }
                if (_this.companyAfterLoadItem) {
                    _this.companyAfterLoadItem.call(_this, this.items);
                }
            },
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    _this.businessDistrict.clearItems();
                } else {
                    _this.businessDistrict.param.fatherid = this.getValue();
                    _this.businessDistrict.reload();
                }
                _this.departs.clearItems();
                _this.emp.clearItems();
                if (_this.companyAfterSelected) {
                    _this.companyAfterSelected.call(_this, this.getValue());
                }
            }
        });
        //公司
        this.company = new Chenxin.UI.StructCombo(config);


        //商圈
        config = {};
        Chenxin.apply(config, this.comboConfig.business);
        Chenxin.applyIf(config, {
            autoLoad: false,
            colWidth: 100,
            param: { op: 'getbusinessdistrict' },
            tbxWidth: this.tbxWidth,
            inModelType: this.inModelType,
            name: this.comboNameConfig.businessDistrict,
            txtModelName: this.comboTxtModelNameConfig.businessDistrict,
            containerId: this.comboContainerId.businessDistrict,
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    _this.departs.clearItems();
                } else {
                    _this.departs.param.fatherid = this.getValue();
                    _this.departs.reload();
                }
                _this.emp.clearItems();
                if (_this.businessDistrictAfterSelected) {
                    _this.businessDistrictAfterSelected.call(_this, this.getValue());
                }
            }
        });

        this.businessDistrict = new Chenxin.UI.StructCombo(config);



        //门店
        config = {};
        Chenxin.apply(config, this.comboConfig.departs);
        Chenxin.applyIf(config, {
            autoLoad: false,
            name: this.comboNameConfig.departs,
            tbxWidth: this.tbxWidth,
            colWidth: 100,
            inModelType: this.inModelType,
            containerId: this.comboContainerId.departs,
            txtModelName: this.comboTxtModelNameConfig.departs,
            param: { op: 'getdeparts' },
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    _this.emp.clearItems();
                } else {
                    _this.emp.param.fatherid = this.getValue();
                    _this.emp.reload();
                }
                if (_this.departsAfterSelected) {
                    _this.departsAfterSelected.call(_this, this.getValue());
                }
            }
        });
        this.departs = new Chenxin.UI.StructCombo(config);
        
        //人
        config = {};
        Chenxin.apply(config, this.comboConfig.emp);
        Chenxin.applyIf(config, {
            autoLoad: false,
            colWidth: 120,
            hasPage:true,
            tbxWidth: this.tbxWidth,
            inModelType: this.inModelType,
            name: this.comboNameConfig.employee,
            txtModelName: this.comboTxtModelNameConfig.employee,
            containerId: this.comboContainerId.employee,
            param: { op: 'getemployee' }
        });
        this.emp = new Chenxin.UI.StructCombo(config);

        return [this.city, this.company, this.businessDistrict, this.departs, this.emp];
    },
    initComponent: function() {
        Chenxin.applyIf(this.comboNameConfig, {
            city: 'cityid',
            company: 'companyid',
            businessDistrict: 'areaid',
            departs: 'departid',
            employee: 'userid'
        });
        Chenxin.applyIf(this.comboTxtModelNameConfig, {
            city: 'city',
            company: 'company',
            businessDistrict: 'area',
            departs: 'depart',
            employee: 'user'
        });
        Chenxin.applyIf(this.comboConfig, {
            city: {},
            company: {},
            business: {},
            departs: {},
            emp: {}
        });
        this.comboList = this.createComboList();
    }
});